package com.bw.sofa_module.videosofa;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bw.adapter_sdk.quick.BaseQuickAdapter;
import com.bw.common.entity.LiveDataEntity;
import com.bw.common.mvvm.view.BaseFragment;
import com.bw.ijk.widget.VideoView;
import com.bw.network_sdk.videoentity.VideoEntity;
import com.bw.network_sdk.videoentity.VideoValuesEntity;
import com.bw.sofa_module.R;
import com.bw.sofa_module.databinding.FragmentVideosBinding;

import java.io.Serializable;

public class VideosFragment extends BaseFragment<FragmentVideosBinding,VideoViewModel> implements BaseQuickAdapter.OnItemClickListener {

    private VideoAdapter videoAdapter;

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        return FragmentVideosBinding.inflate(inflater);
    }

    @Override
    public void initView() {
        v.videosofaRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        videoAdapter = new VideoAdapter();
        v.videosofaRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE){
                    //停止状态->加载当前页中顶部显示的视频->获取顶部显示的item进行播放
                    LinearLayoutManager recylerManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    int firstPosittion = recylerManager.findFirstCompletelyVisibleItemPosition();
                    //显示当前item
                    View view = recylerManager.findViewByPosition(firstPosittion);
                    if (view!=null){
                        VideoView videoView = view.findViewById(R.id.item_video_ijkview);
                        String url = videoAdapter.getData().get(firstPosittion).url;
                        videoView.setPlayUrl(url);
                    }

                }else if (newState == RecyclerView.SCROLL_STATE_SETTLING||newState == RecyclerView.SCROLL_STATE_DRAGGING){
                    //手指抛动->recyclerview惯性滑动动作
                    //全部正在播放的item->停止播放
                    LinearLayoutManager recylerManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    for (int i = 0;i < recyclerView.getAdapter().getItemCount();i++){
                        View view = recylerManager.findViewByPosition(i);
                        if (view!=null){
                            VideoView videoView = view.findViewById(R.id.item_video_ijkview);
                            if (videoView.isPlaying())
                                videoView.stop();
                        }
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        videoAdapter.setOnItemClickListener(this);
        v.videosofaRecycler.setAdapter(videoAdapter);
    }

    @Override
    public void initData() {

    }

    @Override
    public void handleMessage(LiveDataEntity entity) {
        switch (entity.action) {
            case START_VIEW_MODEL:
                VideoEntity videoEntity = (VideoEntity) entity.data.get(LiveDataEntity.LiveDataType.ENTITY);
                videoAdapter.setNewInstance(videoEntity.data.data);
                break;
        }
    }

    @Override
    public void onItemClick(RecyclerView.Adapter adapter, int position) {
        if (adapter instanceof VideoAdapter){
            VideoAdapter videoAdapter = (VideoAdapter) adapter;
            VideoValuesEntity videoValuesEntity = videoAdapter.getData().get(position);
            Bundle bundle = new Bundle();
            bundle.putSerializable("videoEntity", (Serializable) videoValuesEntity);
            Log.i("ggg",videoValuesEntity.toString());
            ARouter.getInstance().build("/sofa/videodetail")
                    .withBundle("bundle",bundle)
                    .navigation(getContext());
        }
    }
}
